package com.kayeah82.application.avatar.qq.service.impl;

import com.kayeah82.application.avatar.qq.component.QQAvatarUtils;
import com.kayeah82.application.avatar.qq.model.Avatar;
import com.kayeah82.application.avatar.qq.repository.AvatarRepository;
import com.kayeah82.application.avatar.qq.service.AvatarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author kayeah82
 * */
@Service
class AvatarServiceImpl implements AvatarService {

    private final AvatarRepository avatarRepository;

    @Autowired
    AvatarServiceImpl(AvatarRepository avatarRepository) {
        this.avatarRepository = avatarRepository;
    }

    @Override
    public Avatar getAvatar(String qq) {
        return avatarRepository.loadAvatar(qq).orElseGet(() -> this.updateAvatar(qq));
    }

    @Override
    public Avatar updateAvatar(String qq) {
        Avatar avatar = QQAvatarUtils.getAvatar(qq);
        avatarRepository.storeAvatar(qq, avatar);
        return avatar;
    }

}
